/*
 * @Author: gyc
 * @Date: 2020-04-09 16:02:05
 * @LastEditTime: 2020-05-22 09:30:00
 * @Description: (ORACLE连接模板)测试版
 */
//取消回调
var oracledb = require('oracledb');
var query = function (sql, callback) {
    return new Promise((resolve, reject) => {
        oracledb.getConnection({
                user: "dsdata",
                password: "dsdata",
                connectString: "192.168.2.238/topprd"
            },
            function (err, connection) {
                if (err) {
                    console.error(err.message);
                    reject(err);
                } else {
                    console.log("连接成功");
                }
                connection.execute(sql, [], function (err, result) {
                    if (err) {
                        console.error(err.message);
                        doRelease(connection);
                        reject(err);
                    }
                    //console.log(result.metaData);
                    resolve(result)
                    // callback(result.rows.map((v) => {
                    //     return result.metaData.reduce((p, key, i) => {
                    //         p[key.name] = v[i];
                    //         resolve(p)
                    //     }, {})
                    // }));
                    doRelease(connection);
                });
            }
        );
    })
}

function doRelease(connection) {
    connection.close(
        function (err) {
            if (err)
                console.error(err.message);
        });
}
exports.query = query;